package com.launch.wlfw.controller;

import com.github.pagehelper.PageInfo;
import com.launch.wlfw.common.Result;
import com.launch.wlfw.pojo.Notice;
import com.launch.wlfw.pojo.TypeInfo;
import com.launch.wlfw.service.NoticeService;
import com.launch.wlfw.util.DataInfo;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @ClassName NoticeController
 * @Description
 * @Author 29172
 * @Date 2023/7/3 18:15
 */

@Controller
public class NoticeController {
    @Resource
    private NoticeService noticeService;

    /**
     * 加载读者的jsp页面
     * @return
     */
    @RequestMapping("noticeIndexOfBack")
    public String typeIndex1(){
        return "notice/noticeIndexOfBack";
    }

    // 加载管理员JSP页面的数据并主页展示
    @RequestMapping("/noticeAll")
    @ResponseBody // 返回的是JSON数据
    // Model数据 ，  View 视图/页面
    /**
     * public class DataInfo implements Serializable，
     * 对象转化成成字节序列就是对象的序列化，而字节序列转化成对象就是对象的反序列化
     */
    public DataInfo list(Notice notice ,
                         @RequestParam("page") Integer pageNum,
                         @RequestParam("limit") Integer pageSize){
        PageInfo<Notice> allInfo = noticeService.findAllInfo(notice, pageNum, pageSize);
        return DataInfo.ok(allInfo);
    }

    //跳转到添加页面
    @RequestMapping("noticeAdd")
    public String toAddPaper() {
        return "/notice/noticeAdd";
    }

    //新增公告信息
    @RequestMapping("/addNoticeSubmit")
    @ResponseBody
    public DataInfo addTypeInfo(Notice notice , HttpServletRequest httpServletRequest){
        HttpSession session = httpServletRequest.getSession();
        String username = session.getAttribute("username").toString();
        notice.setAuthor(username);
        noticeService.addInfo(notice);
        return DataInfo.ok(notice);
    }

    //删除公告信息
    @RequestMapping("/deleteNoticeByIds")
    @ResponseBody
    public Result<Integer> delTypeByID(String ids){
        Integer integer = noticeService.delInfoByID(ids);
        return Result.success(integer);
    }

    //跳转到修改公告页面
    @RequestMapping("queryNoticeById")
    public String queryTypeInfoById(Long id , Model model){
        Notice notice = noticeService.queryById(id);
        model.addAttribute("info" , notice);
        return "notice/updateNotice";
    }

    @RequestMapping("/updateNoticeSubmit")
    @ResponseBody // 返回JSON 数据
    // get ： 参数拼接在url
    // post ： 参数在请求体 ， @RequestBody : 获取请求体的数据
    public DataInfo updateInfo(@RequestBody Notice notice){
        noticeService.updateInfo(notice);
        return DataInfo.ok(notice);
    }

}
